<template>
  <div>

    <el-dialog :title="isAdd ? '添加菜单' : '编辑菜单'" :visible.sync="isShow" :before-close="cancel" center>
      <el-form :model="meuuFrom" :rules="rules" ref="menuRef">
        <el-form-item prop="title" label="菜单名称" :label-width="formLabelWidth">
          <el-input v-model="meuuFrom.title" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item prop="pid" label="上级菜单" :label-width="formLabelWidth">

          <el-select @change="Pid" v-model="meuuFrom.pid" placeholder="请选择">
            <el-option label="顶级菜单" :value="0"></el-option>
            <!-- options 更新啊:用仓库的方法qu -->
            <el-option v-for="item in $store.getters['menu/getMenu']" :key="item.id" :label="item.title" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="菜单类型" :label-width="formLabelWidth">
          <el-radio disabled v-model="meuuFrom.type" :label="1">目录</el-radio>
          <el-radio disabled v-model="meuuFrom.type" :label="2">菜单</el-radio>
        </el-form-item>
        <el-form-item v-if="meuuFrom.type == 1" label="菜单图标" :label-width="formLabelWidth">
          <!-- <el-input v-model="meuuFrom.icon" autocomplete="off"></el-input> -->
          <el-select v-model="meuuFrom.icon" placeholder="请选择">
            <el-option v-for="item in iconArr" :key="item" :label="item" :value="item">
              <i :class="item"></i>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-else label="菜单地址" :label-width="formLabelWidth">
          <!-- <el-input v-model="meuuFrom.url" autocomplete="off"></el-input> -->
          <el-select v-model="meuuFrom.url" placeholder="请选择">
            <el-option v-for="item in routesArr" :key="item.path" :label="item.path" :value="item.path">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch v-model="meuuFrom.status" active-color="#13ce66" inactive-color="#ff4949" :active-value="1"
            :inactive-value="2">
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button @click="add" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
<script>
//引入封装好的接口
import { menuAdd, menuInfo, menuEdit } from "../../request/api";
import { mapActions } from 'vuex'
import { routesArr } from '../../router'
export default {
  props: ["isShow", "isAdd"],
  data() {
    return {
      routesArr,
      iconArr :[
        'el-icon-s-tools',
        'el-icon-setting',
        'el-icon-s-goods',
        'el-icon-goods',
      ],
      formLabelWidth: "120px",
      //表单对象
      meuuFrom: {
        pid: 0, //上级分类编号  注意：顶级菜单-0number
        title: "", //菜单名称string
        icon: "", //图标string
        type: 1, //类型 1目录2菜单number
        url: "", //菜单地址string
        status: 1, //状态   1正常2禁用   number类型number
      },
      rules: {
        title: [
          { required: true, message: "请输入菜单名称", trigger: "blur" },
          { min: 2, max: 8, message: "长度在 2 到 8 个字符", trigger: "blur" },
        ],
        pid: [{ required: true, message: "请选择上级菜单", trigger: "change" }],
      },

    };
  },
  mounted(){
    console.log(routesArr);
  },
  methods: {
    // pid 联动事件
    Pid() {
      this.meuuFrom.type = this.meuuFrom.pid == 0 ? 1 : 2
    },



    ...mapActions({
      getMenuList: "menu/getMenuListAction",
    }),
    //封装一个取消事件
    cancel() {
      //子传父
      this.$emit("cancel", false);
      this.reset();
    },
    //封装一个清空事件
    reset() {
      this.meuuFrom = {
        pid: 0, //上级分类编号  注意：顶级菜单-0number
        title: "", //菜单名称string
        icon: "", //图标string
        type: 1, //类型 1目录2菜单number
        url: "", //菜单地址string
        status: 1, //状态   1正常2禁用   number类型number
      };
      //清空规则验证
      //resetFields	对整个表单进行重置，将所有字段值重置为初始值并移除校验结果
      this.$refs.menuRef.resetFields();
    },
    //封装一个获取一条数据事件，数据回显
    lookup(id) {
      // console.log(id,'准备回显数据')
      menuInfo({ id }).then((res) => {
        console.log(res, "获取一条数据");
        this.meuuFrom = res.list;
        this.meuuFrom.id = id
      });
    },
    //封装一个表单添加事件
    add() {
      this.$refs.menuRef.validate((val) => {
        if (val) {
          // console.log(this.meuuFrom, "表单对象");
          if (this.isAdd) {
            //调用添加接口
            menuAdd(this.meuuFrom).then((res) => {
              if (res.code == 200) {
                //关闭弹框
                console.log('添加');
                this.cancel();
                //重新调用列表 =>触发行动
                this.getMenuList()
                this.$message.success(res.msg);
              } else {
                this.$message.error(res.msg);
              }
            });
          } else {
            //调用的是编辑接口
            menuEdit(this.meuuFrom).then((res) => {
              if (res.code == 200) {
                //关闭弹框
                this.cancel();
                //重新调用列表 =>触发行动
                this.$store.dispatch("meuu/getMenuListAction");
                this.$message.success(res.msg);
              } else {
                this.$message.error(res.msg);
              }
            });
          }
        }
      });
    },
  },
};
</script>
  
<style scoped></style>
  